import { defineStore } from 'pinia';
import { getUserSettings, switchBook, updateSettings } from '@/api/setting';
import { useUserStore } from './user';

export const useSettingsStore = defineStore('settings', {
  state: () => ({
    // 基本设置
    studyType: 1, // 1:雅思，2:托福
    wordCount: 10, // 关卡背单词数
    isGuide: true, // 是否需要引导
    
    // 雅思设置
    ieltsReadingLength: 500, // 雅思文章单词数
    ieltsReadingQuestionCount: 4, // 雅思文章单个题型问题数
    ieltsListeningLength: 300, // 雅思听力单词数
    ieltsListeningQuestionCount: 4, // 雅思听力问题数
    
    // 托福设置
    toeflReadingLength: 200, // 托福阅读单词数
    toeflReadingQuestionCount: 3, // 托福阅读问题数
    toeflListeningLength: 300, // 托福听力单词数
    toeflListeningQuestionCount: 4, // 托福听力问题数
    
    // 状态标记
    loading: false,
    initialized: false,
  }),
  
  getters: {
    currentBook: (state) => state.studyType === 1 ? '雅思英语' : '托福英语',
    
    // 根据当前选择的考试类型返回对应设置
    currentReadingLength: (state) => 
      state.studyType === 1 ? state.ieltsReadingLength : state.toeflReadingLength,
    
    currentReadingQuestionCount: (state) => 
      state.studyType === 1 ? state.ieltsReadingQuestionCount : state.toeflReadingQuestionCount,
    
    currentListeningLength: (state) => 
      state.studyType === 1 ? state.ieltsListeningLength : state.toeflListeningLength,
    
    currentListeningQuestionCount: (state) => 
      state.studyType === 1 ? state.ieltsListeningQuestionCount : state.toeflListeningQuestionCount,
  },
  
  actions: {
    async fetchSettings() {
      try {
        this.loading = true;
        
        const response = await getUserSettings();
        
        if (response && response.code === 0 && response.data) {
          const data = response.data;
          
          // 更新基本设置
          this.studyType = data.study_type || 1;
          this.wordCount = data.word_count || 10;
          this.isGuide = data.is_guide !== undefined ? data.is_guide : true;
          
          // 更新雅思设置
          this.ieltsReadingLength = data.ielts_reading_length || 500;
          this.ieltsReadingQuestionCount = data.ielts_reading_question_count || 4;
          this.ieltsListeningLength = data.ielts_listening_length || 300;
          this.ieltsListeningQuestionCount = data.ielts_listening_question_count || 4;
          
          // 更新托福设置
          this.toeflReadingLength = data.toefl_reading_length || 200;
          this.toeflReadingQuestionCount = data.toefl_reading_question_count || 3;
          this.toeflListeningLength = data.toefl_listening_length || 300;
          this.toeflListeningQuestionCount = data.toefl_listening_question_count || 4;
          
          // 同时更新用户存储中的book_id
          const userStore = useUserStore();
          if (userStore.user) {
            userStore.user.book_id = this.studyType;
          }
        }
        
        this.initialized = true;
      } catch (error) {
        console.error('获取设置失败:', error);
      } finally {
        this.loading = false;
      }
    },
    
    async changeStudyType(studyType) {
      if (studyType === this.studyType) return;
      
      try {
        this.loading = true;
        
        // 调用API更新学习类型
        await updateSettings({ study_type: studyType });
        
        this.studyType = studyType;
        
        // 同时更新用户存储中的book_id
        const userStore = useUserStore();
        if (userStore.user) {
          userStore.user.book_id = studyType;
        }
        
        console.log('学习类型切换成功');
      } catch (error) {
        console.error('切换学习类型失败:', error);
      } finally {
        this.loading = false;
      }
    },
    
    async updateUserSettings(settings) {
      try {
        this.loading = true;
        
        // 将驼峰命名转换为下划线命名
        const apiSettings = {};
        
        if (settings.studyType !== undefined) apiSettings.study_type = settings.studyType;
        if (settings.wordCount !== undefined) apiSettings.word_count = settings.wordCount;
        if (settings.isGuide !== undefined) apiSettings.is_guide = settings.isGuide;
        
        if (settings.ieltsReadingLength !== undefined) apiSettings.ielts_reading_length = settings.ieltsReadingLength;
        if (settings.ieltsReadingQuestionCount !== undefined) apiSettings.ielts_reading_question_count = settings.ieltsReadingQuestionCount;
        if (settings.ieltsListeningLength !== undefined) apiSettings.ielts_listening_length = settings.ieltsListeningLength;
        if (settings.ieltsListeningQuestionCount !== undefined) apiSettings.ielts_listening_question_count = settings.ieltsListeningQuestionCount;
        
        if (settings.toeflReadingLength !== undefined) apiSettings.toefl_reading_length = settings.toeflReadingLength;
        if (settings.toeflReadingQuestionCount !== undefined) apiSettings.toefl_reading_question_count = settings.toeflReadingQuestionCount;
        if (settings.toeflListeningLength !== undefined) apiSettings.toefl_listening_length = settings.toeflListeningLength;
        if (settings.toeflListeningQuestionCount !== undefined) apiSettings.toefl_listening_question_count = settings.toeflListeningQuestionCount;
        
        // 调用API更新设置
        await updateSettings(apiSettings);
        
        // 更新本地状态
        Object.assign(this, settings);
        
        // 如果更新了学习类型，同时更新用户存储中的book_id
        if (settings.studyType !== undefined) {
          const userStore = useUserStore();
          if (userStore.user) {
            userStore.user.book_id = settings.studyType;
          }
        }
        
        console.log('设置更新成功');
      } catch (error) {
        console.error('更新设置失败:', error);
      } finally {
        this.loading = false;
      }
    },
    
    // 兼容旧版API，保留changeBook方法
    async changeBook(bookType) {
      return this.changeStudyType(bookType);
    }
  }
}); 